<template>
  <div class="edit-page-wrap">
    <Head title="告警信息">
      <template #content>
        <el-form
          ref="ruleFormRef"
          :model="ruleForm"
          :rules="rules"
          label-width="140px"
          class="edit-form-wrap"
        >
          <el-form-item label="告警名称" prop="alarmSettingName">
            <el-input
              v-if="!id"
              v-model.trim="ruleForm.alarmSettingName"
              placeholder="请输入告警名称"
              maxlength="30"
              clearable
            />
            <div v-else>{{ ruleForm.alarmSettingName }}</div>
          </el-form-item>

          <el-form-item label="告警类型" prop="alarmSettingType" v-if="!id">
            <el-radio-group
              v-model="ruleForm.alarmSettingType"
              :disabled="Boolean(id)"
              v-for="(item, index) in optionsS.alarmSettingTypeData"
              :key="index"
            >
              <el-radio :label="item.value">{{ item.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="告警类型" prop="alarmSettingType" v-else>
            <div>
              {{
                findKeyByValue(
                  optionsS.alarmSettingTypeData1,
                  ruleForm.alarmSettingType
                )
              }}
            </div>
          </el-form-item>
          <div
            v-if="
              ruleForm.alarmSettingType == '1' ||
              ruleForm.alarmSettingType == '2'
            "
          >
            <el-form-item
              v-if="ruleForm.alarmSettingType === '1'"
              label="适用电桩"
              prop="relateIds"
            >
              <QcTree
                ref="treeEquipmentRef"
                :setCheckedKeys="ruleForm.relateIds"
                :data="optionsS.equipmentTreeData"
                :showCheckbox="true"
                @handleCheck="handleEquipment"
              ></QcTree>
            </el-form-item>

            <el-form-item
              v-if="ruleForm.alarmSettingType === '2'"
              label="适用企业"
              prop="relateIds"
            >
              <QcTree
                ref="treeEquipmentRef"
                :setCheckedKeys="ruleForm.relateIds"
                :data="optionsS.companyTreeData"
                :showCheckbox="true"
                :checkStrictly="true"
                @handleCheck="handleEquipment"
              ></QcTree>
            </el-form-item>
          </div>
          <div
            v-if="
              ruleForm.alarmSettingType == '3' ||
              ruleForm.alarmSettingType == '4'
            "
          ></div>
          <el-form-item
            label="通知号码"
            prop="notifyNumbers"
            class="has-tips"
            v-if="ruleForm.alarmSettingType !== '3'"
          >
            <el-input
              v-model.trim="ruleForm.notifyNumbers"
              placeholder="请填写手机号码,例如13888888888,13666666666"
              type="textarea"
              :rows="3"
              show-word-limit
              clearable
            />
            <div class="tip">
              <div class="icon"></div>
              告警项勾选了“是否告警”和"告警短信"
              时，一旦触发告警即给通知号码发送告警短信提醒
            </div>
          </el-form-item>
        </el-form>
      </template>
    </Head>
    <div class="edit-line"></div>

    <Head title="告警设置" v-if="ruleForm.alarmSettingType == '1'">
      <template #content>
        <div class="tips-info">
          <div class="info-item">设置说明</div>
          <div class="info-item">
            1、选中“是否告警”，平台才会按设置的告警阈值生成告警记录，并根据是否选中“告警短信”、“自动停充”来给通知号码发送短信通知、下发停充指令。
          </div>
          <div class="info-item">
            2、多级别告警项，级别数值越大表示情况越危急；建议按级别设置是否需要短信通知、下发停充。
          </div>
        </div>

        <div class="message-card">
          <div class="message-title">电桩充电告警</div>
          <el-form
            ref="ruleFormRef1"
            :model="ruleForm"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template v-for="(sitem, index) in ruleForm.dataForm" :key="index">
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable"
                  :prop="'dataForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <el-input
                    v-model.trim="sitem.threshold"
                    placeholder="请输入"
                    style="width: 280px"
                    maxlength="20"
                  >
                    <template #prepend>{{ sitem.labelName }}</template>
                    <template #append>{{ sitem.unit }}</template>
                  </el-input>
                </el-form-item>
                <el-form-item label="" :prop="sitem.alarmCheckType">
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData"
                      :key="item.value"
                      :label="item.label"
                      :disabled="
                        item.label == '告警短信' || item.label == '自动停充'
                          ? item.value == '1'
                          : false
                      "
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
        <div class="message-card">
          <div class="message-title">
            车辆告警
            <span style="color: #eb5e12"
              >（注：以下告警项仅对直流设备有效）</span
            >
          </div>
          <el-form
            ref="ruleFormRef2"
            :model="ruleForm"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template
              v-for="(sitem, index) in ruleForm.carDataForm"
              :key="index"
            >
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable"
                  :prop="'carDataForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <el-input
                    v-model.trim="sitem.threshold"
                    placeholder="请输入"
                    style="width: 280px"
                    maxlength="20"
                  >
                    <template #prepend>{{ sitem.labelName }}</template>
                    <template #append>{{ sitem.unit }}</template>
                  </el-input>
                </el-form-item>
                <el-form-item
                  label=""
                  :prop="sitem.alarmCheckType"
                  v-if="sitem.threshold"
                >
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting1(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData"
                      :key="item.value"
                      :label="item.label"
                      :disabled="
                        item.label == '告警短信' || item.label == '自动停充'
                          ? item.value == '1'
                          : false
                      "
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
        <div class="message-card">
          <div class="message-title">连续启动失败告警</div>
          <el-form
            ref="ruleFormRef3"
            :model="ruleForm"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template
              v-for="(sitem, index) in ruleForm.failDataForm"
              :key="`${index}`"
            >
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable"
                  :prop="'failDataForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <el-input
                    v-model.trim="sitem.threshold"
                    placeholder="请输入"
                    style="width: 280px"
                    maxlength="20"
                  >
                    <template #prepend>{{ sitem.labelName }}</template>
                    <template #append>{{ sitem.unit }}</template>
                  </el-input>
                </el-form-item>
                <el-form-item
                  label=""
                  :prop="sitem.alarmCheckType"
                  v-if="sitem.threshold"
                >
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting2(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData1"
                      :key="item.value"
                      :label="item.label"
                      :disabled="
                        item.label == '告警短信' ? item.value == '1' : false
                      "
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
      </template>
    </Head>
    <Head title="告警设置" v-if="ruleForm.alarmSettingType == '2'">
      <template #content>
        <div class="tips-info">
          <div class="info-item">设置说明</div>
          <div class="info-item">
            选中“是否告警”，“适用企业”的用户账户余额低低于阈值时，平台才会生成告警记录，并根据是否选中“告警短信”来给通知号码发送短信通知。
          </div>
        </div>
        <div class="message-card">
          <div class="message-title">账户告警</div>
          <el-form
            ref="ruleFormRef4"
            :model="ruleForm"
            :rules="rules"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template
              v-for="(sitem, index) in ruleForm.accountDataForm"
              :key="index"
            >
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable"
                  :prop="'accountDataForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <el-input
                    v-model.trim="sitem.threshold"
                    placeholder="请输入"
                    style="width: 280px"
                    maxlength="20"
                  >
                    <template #prepend>{{ sitem.labelName }}</template>
                    <template #append>{{ sitem.unit }}</template>
                  </el-input>
                </el-form-item>
                <el-form-item
                  label=""
                  :prop="sitem.alarmCheckType"
                  v-if="sitem.threshold"
                >
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting3(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData1"
                      :key="item.value"
                      :label="item.label"
                      :disabled="
                        item.label == '告警短信' ? item.value == '1' : false
                      "
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
      </template>
    </Head>
    <Head title="告警设置" v-if="ruleForm.alarmSettingType == '3'">
      <template #content>
        <div class="tips-info">
          <div class="info-item">设置说明</div>
          <div class="info-item">
            1、选中“是否告警”，平台才会生成告警记录，并是否选中“告警短信”来给用户发送短信通知。
          </div>
        </div>
        <div class="message-card">
          <div class="message-title">异常停充告警</div>
          <el-form
            ref="ruleFormRef5"
            :model="ruleForm"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template
              v-for="(sitem, index) in ruleForm.abnormalForm"
              :key="index"
            >
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable"
                  :prop="'dataFoabnormalForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <div style="width: 280px"></div>
                </el-form-item>
                <el-form-item label="" :prop="sitem.alarmCheckType">
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting4(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData1"
                      :key="item.value"
                      :label="item.label"
                      :disabled="
                        item.label == '告警短信' ? item.value == '1' : false
                      "
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
        <div class="message-card">
          <div class="message-title">连续启动失败告警</div>
          <el-form
            ref="ruleFormRef3"
            :model="ruleForm"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template
              v-for="(sitem, index) in ruleForm.failDataForm"
              :key="`${index}`"
            >
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable1"
                  :prop="'failDataForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <el-input
                    v-model.trim="sitem.threshold"
                    placeholder="请输入"
                    style="width: 280px"
                    maxlength="20"
                  >
                    <template #prepend>{{ sitem.labelName }}</template>
                    <template #append>{{ sitem.unit }}</template>
                  </el-input>
                </el-form-item>
                <el-form-item
                  label=""
                  :prop="sitem.alarmCheckType"
                  v-if="sitem.threshold"
                >
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting2(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData1"
                      :key="item.value"
                      :label="item.label"
                      :disabled="
                        item.label == '告警短信' ? item.value == '1' : false
                      "
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
      </template>
    </Head>
    <Head title="告警设置" v-if="ruleForm.alarmSettingType == '4'">
      <template #content>
        <div class="tips-info">
          <div class="info-item">设置说明</div>
          <div class="info-item">
            1、告警项勾选了“是否告警”和"告警短信"
            时，一旦触发告警即给通知号码发送告警短信提醒
          </div>
          <div class="info-item">2、电桩故障告警为设备上送的告警信息</div>
        </div>
        <div class="message-card">
          <div class="message-title">电桩离线告警</div>
          <el-form
            ref="ruleFormRef6"
            :model="ruleForm"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template
              v-for="(sitem, index) in ruleForm.plieDataForm"
              :key="`${index}`"
            >
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable"
                  :prop="'plieDataForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <el-input
                    v-model.trim="sitem.threshold"
                    placeholder="请输入"
                    style="width: 280px"
                    maxlength="20"
                  >
                    <template #prepend>{{ sitem.labelName }}</template>
                    <template #append>{{ sitem.unit }}</template>
                  </el-input>
                </el-form-item>
                <el-form-item label="" :prop="sitem.alarmCheckType">
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting5(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData1"
                      :key="item.value"
                      :label="item.label"
                      :disabled="
                        item.label == '告警短信' ? item.value == '1' : false
                      "
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
        <div class="message-card">
          <div class="message-title">电桩故障告警</div>
          <el-form
            ref="ruleFormRef7"
            :model="ruleForm"
            label-width="140px"
            class="edit-form-wrap"
          >
            <template
              v-for="(sitem, index) in ruleForm.plieFaultDataForm"
              :key="index"
            >
              <div class="flex-form">
                <el-form-item
                  :label="sitem.dataFormLable"
                  :prop="'plieFaultDataForm.' + index + '.threshold'"
                  :rules="sitem.itemRules"
                >
                  <div style="width: 280px"></div>
                </el-form-item>
                <el-form-item label="" :prop="sitem.alarmCheckType">
                  <el-checkbox-group
                    v-model="sitem.alarmCheckType"
                    @change="handleChangeSetting6(index, sitem.alarmCheckType)"
                  >
                    <el-checkbox
                      v-for="item in optionsS.settingData"
                      :key="item.value"
                      :label="item.label"
                    >
                    </el-checkbox>
                  </el-checkbox-group>
                </el-form-item>
              </div>
            </template>
          </el-form>
        </div>
      </template>
    </Head>
    <div class="edit-line"></div>
    <div class="edit-btn-wrap">
      <el-button type="primary" plain @click="back">取消</el-button>
      <el-button type="primary" v-btnAntiShake="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script lang="ts" setup>
import type { FormInstance, FormRules } from 'element-plus';
import Head from '@/components/head/index.vue';
import QcTree from '@/components/qcTree/index.vue';
import {
  saveAlarmSetting,
  updateAlarmSetting,
  getAlarmSet,
} from '@/service/apis/alarm';
import { equipmentTreeDataNoPriceSet, queryAllInst } from '@/service/apis/org';
import {
  IS_NUM,
  IS_more_2,
  IS_TWO_DECIMAL,
  IS_INT_0_DOT,
  IS_INT,
} from '@/utils/verify';
import { findKeyByValue } from '@/utils/utils';
import { queryDicByParent } from '@/service/apis/api';

const router = useRouter();
const route = useRoute();
const {
  query: { id },
} = route;
const ruleFormRef = ref<FormInstance>();
const ruleFormRef1 = ref<FormInstance>();
const ruleFormRef2 = ref<FormInstance>();
const ruleFormRef3 = ref<FormInstance>();
const ruleFormRef4 = ref<FormInstance>();
const ruleFormRef5 = ref<FormInstance>();
const ruleFormRef6 = ref<FormInstance>();
const ruleFormRef7 = ref<FormInstance>();

let optionsS = reactive({
  alarmSettingTypeData: [] as any,
  alarmSettingTypeData1: [] as any,
  settingData: [
    {
      label: '是否告警',
      value: '1',
    },
    {
      label: '告警短信',
      value: '2',
    },
    {
      label: '自动停充',
      value: '3',
    },
  ],
  settingData1: [
    {
      label: '是否告警',
      value: '1',
    },
    {
      label: '告警短信',
      value: '2',
    },
  ],
  equipmentTreeData: [] as any,
  companyTreeData: [] as any,
  alarmTermsArr: [] as any,
});

let ruleForm: any = reactive({
  id: undefined as unknown,
  alarmSettingName: '',
  alarmSettingType: '',
  relateIds: [] as any,
  notifyNumbers: '',
  alarmTerms: [] as any,
  dataForm: [] as any,
  carDataForm: [] as any,
  failDataForm: [] as any,
  accountDataForm: [] as any,
});

const checkNotifyNumbers = (rule: any, value: any, callback: any) => {
  const alarmTermsArr = [
    ...ruleForm.dataForm,
    ...ruleForm.carDataForm,
    ...ruleForm.failDataForm,
    ...ruleForm.accountDataForm,
  ];
  alarmTermsArr.filter((item: any) => {
    if (item.isNotify == 1 && value == '') {
      callback(new Error('请填写通知号码'));
      return;
    }
  });
  callback();
};

const rules = reactive<FormRules>({
  alarmSettingName: [
    { required: true, message: '请填写告警名称', trigger: 'blur' },
  ],
  alarmSettingType: [
    { required: true, message: '请选择告警类型', trigger: 'change' },
  ],
  relateIds: [
    { required: true, message: '请选择适用电桩', trigger: ['blur', 'change'] },
  ],
  notifyNumbers: [
    { required: false, message: '请填写通知号码', trigger: 'blur' },
    {
      pattern: IS_INT_0_DOT,
      message: '仅支持输入数字和英文逗号',
      trigger: 'blur',
    },
    { validator: checkNotifyNumbers, trigger: 'change' },
  ],
});

ruleForm.dataForm = [
  {
    dataFormLable: '枪线温度过高',
    labelName: '一级警告阈值',
    unit: '℃',
    alarmType: 1,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: '℃',
    alarmType: 1,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: '℃',
    alarmType: 1,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '枪线温度过低',
    labelName: '一级警告阈值',
    unit: '℃',
    alarmType: 2,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: '℃',
    alarmType: 2,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: '℃',
    alarmType: 2,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '充电电压过高',
    labelName: '一级警告阈值',
    unit: 'V',
    alarmType: 3,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: 'V',
    alarmType: 3,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: 'V',
    alarmType: 3,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '充电电压过低',
    labelName: '一级警告阈值',
    unit: 'V',
    alarmType: 4,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: 'V',
    alarmType: 4,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: 'V',
    alarmType: 4,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '充电电流过高',
    labelName: '一级警告阈值',
    unit: 'A',
    alarmType: 5,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: 'A',
    alarmType: 5,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: 'A',
    alarmType: 5,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '充电电流过低',
    labelName: '一级警告阈值',
    unit: 'A',
    alarmType: 6,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: 'A',
    alarmType: 6,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: 'A',
    alarmType: 6,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '输入功率不足',
    labelName: '一级警告阈值',
    unit: 'kW',
    alarmType: 7,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: 'kW',
    alarmType: 7,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
];

ruleForm.carDataForm = [
  {
    dataFormLable: '电池电量监控',
    labelName: '一级警告阈值',
    unit: 'KWH',
    alarmType: 13,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: 'KWH',
    alarmType: 13,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: 'KWH',
    alarmType: 13,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: 'SOC监控',
    labelName: '一级警告阈值',
    unit: '%',
    alarmType: 14,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: '%',
    alarmType: 14,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: '%',
    alarmType: 14,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '电池温度监控',
    labelName: '一级警告阈值',
    unit: '℃',
    alarmType: 15,
    alarmLevel: 1,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入一级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '二级警告阈值',
    unit: '℃',
    alarmType: 15,
    alarmLevel: 2,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入二级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
  {
    dataFormLable: '',
    labelName: '三级警告阈值',
    unit: '℃',
    alarmType: 15,
    alarmLevel: 3,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入三级警告阈值', trigger: 'blur' },
      {
        pattern: IS_NUM,
        message: '请输入数字',
        trigger: 'blur',
      },
    ],
  },
];

ruleForm.failDataForm = [
  {
    dataFormLable: '单终端',
    dataFormLable1: '单用户',
    labelName: '连续失败',
    unit: '次',
    alarmType: 12,
    alarmLevel: 0,
    prop: 'threshold',
    threshold: '5',
    itemRules: [
      { required: false, message: '请输入连续失败次数', trigger: 'blur' },
      {
        pattern: IS_more_2,
        message: '请输入大于2的正整数',
        trigger: 'blur',
      },
    ],
  },
];

ruleForm.accountDataForm = [
  {
    dataFormLable: '账户余额告警',
    labelName: '警告阈值',
    unit: '元',
    alarmType: 10,
    alarmLevel: 0,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '0',
    itemRules: [
      { required: false, message: '请输入警告阈值', trigger: 'blur' },
      {
        pattern: IS_TWO_DECIMAL,
        message: '请输入警告阈值，保留2位小数',
        trigger: 'blur',
      },
    ],
  },
];

ruleForm.abnormalForm = [
  {
    dataFormLable: '异常停充告警',
    labelName: '异常停充告警',
    alarmType: 11,
    alarmLevel: 0,
    isNotify: false,
    isAlarm: false,
  },
];

ruleForm.plieDataForm = [
  {
    dataFormLable: '电桩离线告警',
    labelName: '离线时长',
    unit: '分钟',
    alarmType: 8,
    alarmLevel: 0,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
    prop: 'threshold',
    threshold: '',
    itemRules: [
      { required: false, message: '请输入离线时长', trigger: 'blur' },
      {
        pattern: IS_INT,
        message: '请输入正确的离线时长，大于0的正整数',
        trigger: 'blur',
      },
    ],
  },
];

ruleForm.plieFaultDataForm = [
  {
    dataFormLable: '电桩故障告警',
    alarmType: 9,
    alarmLevel: 0,
    isNotify: false,
    isStopCharging: false,
    isAlarm: false,
  },
];

queryDicByParent({
  listDtos: ['AlarmSettingType'],
}).then((res: any) => {
  optionsS.alarmSettingTypeData = res.AlarmSettingType.filter(
    (item: any) => item.value === '1' || item.value === '2'
  );
  optionsS.alarmSettingTypeData1 = res.AlarmSettingType;
});

const handleChangeSetting = (index: any, value: any) => {
  if (value.includes('是否告警')) {
    ruleForm.dataForm[index].isAlarm = true;
  } else {
    ruleForm.dataForm[index].isAlarm = false;
  }
  if (value.includes('告警短信')) {
    ruleForm.dataForm[index].isNotify = true;

    if (value.includes('是否告警')) {
      ruleForm.dataForm[index].alarmCheckType;
    } else {
      ruleForm.dataForm[index].isAlarm = true;
      ruleForm.dataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.dataForm[index].isNotify = false;
  }

  if (value.includes('自动停充')) {
    ruleForm.dataForm[index].isStopCharging = true;
    if (value.includes('是否告警')) {
      ruleForm.dataForm[index].alarmCheckType;
    } else {
      ruleForm.dataForm[index].isAlarm = true;
      ruleForm.dataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.dataForm[index].isStopCharging = false;
  }
  console.log(ruleForm.dataForm);
};

const handleChangeSetting1 = (index: any, value: any) => {
  if (value.includes('是否告警')) {
    ruleForm.carDataForm[index].isAlarm = true;
  } else {
    ruleForm.carDataForm[index].isAlarm = false;
  }
  if (value.includes('告警短信')) {
    ruleForm.carDataForm[index].isNotify = true;

    if (value.includes('是否告警')) {
      ruleForm.carDataForm[index].alarmCheckType;
    } else {
      ruleForm.carDataForm[index].isAlarm = true;
      ruleForm.carDataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.carDataForm[index].isNotify = false;
  }

  if (value.includes('自动停充')) {
    ruleForm.carDataForm[index].isStopCharging = true;
    if (value.includes('是否告警')) {
      ruleForm.carDataForm[index].alarmCheckType;
    } else {
      ruleForm.carDataForm[index].isAlarm = true;
      ruleForm.carDataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.carDataForm[index].isStopCharging = false;
  }
  console.log('carDataForm==========', ruleForm.carDataForm);
};

const handleChangeSetting2 = (index: any, value: any) => {
  if (value.includes('是否告警')) {
    ruleForm.failDataForm[index].isAlarm = true;
  } else {
    ruleForm.failDataForm[index].isAlarm = false;
  }
  if (value.includes('告警短信')) {
    ruleForm.failDataForm[index].isNotify = true;

    if (value.includes('是否告警')) {
      ruleForm.failDataForm[index].alarmCheckType;
    } else {
      ruleForm.failDataForm[index].isAlarm = true;
      ruleForm.failDataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.failDataForm[index].isNotify = false;
  }
};

const handleChangeSetting3 = (index: any, value: any) => {
  if (value.includes('是否告警')) {
    ruleForm.accountDataForm[index].isAlarm = true;
  } else {
    ruleForm.accountDataForm[index].isAlarm = false;
  }
  if (value.includes('告警短信')) {
    ruleForm.accountDataForm[index].isNotify = true;

    if (value.includes('是否告警')) {
      ruleForm.accountDataForm[index].alarmCheckType;
    } else {
      ruleForm.accountDataForm[index].isAlarm = true;
      ruleForm.accountDataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.accountDataForm[index].isNotify = false;
  }
};
const handleChangeSetting4 = (index: any, value: any) => {
  if (value.includes('是否告警')) {
    ruleForm.abnormalForm[index].isAlarm = true;
  } else {
    ruleForm.abnormalForm[index].isAlarm = false;
  }
  if (value.includes('告警短信')) {
    ruleForm.abnormalForm[index].isNotify = true;

    if (value.includes('是否告警')) {
      ruleForm.abnormalForm[index].alarmCheckType;
    } else {
      ruleForm.abnormalForm[index].isAlarm = true;
      ruleForm.abnormalForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.abnormalForm[index].isNotify = false;
  }
};

const handleChangeSetting5 = (index: any, value: any) => {
  if (value.includes('是否告警')) {
    ruleForm.plieDataForm[index].isAlarm = true;
  } else {
    ruleForm.plieDataForm[index].isAlarm = false;
  }
  if (value.includes('告警短信')) {
    ruleForm.plieDataForm[index].isNotify = true;

    if (value.includes('是否告警')) {
      ruleForm.plieDataForm[index].alarmCheckType;
    } else {
      ruleForm.plieDataForm[index].isAlarm = true;
      ruleForm.plieDataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.plieDataForm[index].isNotify = false;
  }
};

const handleChangeSetting6 = (index: any, value: any) => {
  if (value.includes('是否告警')) {
    ruleForm.plieFaultDataForm[index].isAlarm = true;
  } else {
    ruleForm.plieFaultDataForm[index].isAlarm = false;
  }
  if (value.includes('告警短信')) {
    ruleForm.plieFaultDataForm[index].isNotify = true;

    if (value.includes('是否告警')) {
      ruleForm.plieFaultDataForm[index].alarmCheckType;
    } else {
      ruleForm.plieFaultDataForm[index].isAlarm = true;
      ruleForm.plieFaultDataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.plieFaultDataForm[index].isNotify = false;
  }

  if (value.includes('自动停充')) {
    ruleForm.plieFaultDataForm[index].isStopCharging = true;
    if (value.includes('是否告警')) {
      ruleForm.plieFaultDataForm[index].alarmCheckType;
    } else {
      ruleForm.plieFaultDataForm[index].isAlarm = true;
      ruleForm.plieFaultDataForm[index].alarmCheckType.push('是否告警');
    }
  } else {
    ruleForm.plieFaultDataForm[index].isStopCharging = false;
  }
  console.log(ruleForm.plieFaultDataForm);
};

equipmentTreeDataNoPriceSet({}).then((res: any) => {
  if (res) {
    console.log([...res]);
    optionsS.equipmentTreeData = [...res];
  }
});

// 选择企业
queryAllInst({}).then((res: any) => {
  if (res) {
    console.log([...res]);
    optionsS.companyTreeData = [...res];
  }
});

const handleEquipment = (
  data: any,
  checkedData: any,
  checkedChildData: any
) => {
  ruleForm.relateIds = checkedChildData;
  ruleFormRef.value?.validateField('relateIds');
};

// 返回
const back = () => {
  router.go(-1);
};

// 编辑回填数据
if (id) {
  console.log(id);
  getAlarmSet({
    id: id,
  }).then((res: any) => {
    if (res) {
      ruleForm.id = res.id;
      ruleForm.alarmSettingName = res.alarmSettingName;
      ruleForm.relateIds = res.relateIds;
      ruleForm.alarmSettingType = String(res.alarmSettingType);
      ruleForm.notifyNumbers = res.notifyNumbers.join(',');
      const alarmTerms = res.alarmTerms;

      // 判断充电安全告警时告警设置的数据回显
      if (ruleForm.alarmSettingType == '1') {
        // 电桩充电告警数据回显，判断alarmType和alarmLevel相同时，数组合并
        const arrs = alarmTerms.slice(0, 20).map((item: any) => {
          console.log('item=========', item);
          const data = ruleForm.dataForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );
          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }

          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }

          if (item.isStopCharging) {
            item.alarmCheckType.push('自动停充');
          }
          return { ...data[0], ...item };
        });
        ruleForm.dataForm = arrs;

        // 车辆告警数据回显，判断alarmType和alarmLevel相同时，数组合并
        const arrs1 = alarmTerms.slice(21, 30).map((item: any) => {
          const carData = ruleForm.carDataForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );
          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }

          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }

          if (item.isStopCharging) {
            item.alarmCheckType.push('自动停充');
          }
          return { ...carData[0], ...item };
        });
        ruleForm.carDataForm = arrs1;

        // 连续启动失败告警告警数据回显，判断alarmType和alarmLevel相同时，数组合并
        const arrs2 = alarmTerms.slice(20, 21).map((item: any) => {
          const failData = ruleForm.failDataForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );

          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }
          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }
          return { ...failData[0], ...item };
        });
        ruleForm.failDataForm = arrs2;
      }

      if (ruleForm.alarmSettingType == '2') {
        // 电桩充电告警数据回显，判断alarmType和alarmLevel相同时，数组合并
        const arrs = alarmTerms.slice(0, 1).map((item: any) => {
          console.log('item=========', item);
          const data = ruleForm.accountDataForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );
          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }

          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }
          return { ...data[0], ...item };
        });
        ruleForm.accountDataForm = arrs;
      }

      if (ruleForm.alarmSettingType == '3') {
        const arrs1 = alarmTerms.slice(0, 1).map((item: any) => {
          const abnormaData = ruleForm.abnormalForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );

          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }
          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }
          return { ...abnormaData[0], ...item };
        });
        ruleForm.abnormalForm = arrs1;

        const arrs2 = alarmTerms.slice(1, 2).map((item: any) => {
          const failData = ruleForm.failDataForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );
          if (!item.threshold) {
            delete failData[0].threshold;
          }
          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }
          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }
          return { ...failData[0], ...item };
        });
        ruleForm.failDataForm = arrs2;
      }

      if (ruleForm.alarmSettingType == '4') {
        const arrs1 = alarmTerms.slice(0, 1).map((item: any) => {
          const abnormaData = ruleForm.plieDataForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );

          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }
          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }
          return { ...abnormaData[0], ...item };
        });
        ruleForm.plieDataForm = arrs1;

        const arrs2 = alarmTerms.slice(1, 2).map((item: any) => {
          const plieFaultData = ruleForm.plieFaultDataForm.filter(
            (i: any) =>
              item.alarmType == i.alarmType && item.alarmLevel == i.alarmLevel
          );

          item.alarmCheckType = [];
          if (item.isAlarm) {
            item.alarmCheckType.push('是否告警');
          }
          if (item.isNotify) {
            item.alarmCheckType.push('告警短信');
          }
          if (item.isStopCharging) {
            item.alarmCheckType.push('自动停充');
          }
          return { ...plieFaultData[0], ...item };
        });
        ruleForm.plieFaultDataForm = arrs2;
      }
    }
  });
} else {
  ruleForm.alarmSettingType = '1';
}

// 新增/修改提交
const emit = defineEmits(['clear-cache']);
const onSubmit = async () => {
  if (!ruleFormRef.value) return;

  if (ruleForm.alarmSettingType === '1') {
    if (!ruleFormRef1.value || !ruleFormRef2.value || !ruleFormRef3.value)
      return;
    let flag = true;
    let objError = {} as any;
    await Promise.all([
      ruleFormRef.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
      ruleFormRef1.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
      ruleFormRef2.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
      ruleFormRef3.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
    ]);
    if (!flag) {
      for (const key in ruleForm) {
        if (objError[key]) {
          ruleFormRef.value.scrollToField(key);
          ruleFormRef1.value.scrollToField(key);
          ruleFormRef2.value.scrollToField(key);
          ruleFormRef3.value.scrollToField(key);
          break;
        }
      }
      return;
    }
  }

  if (ruleForm.alarmSettingType === '2') {
    if (!ruleFormRef4.value) return;
    let flag = true;
    let objError = {} as any;
    await Promise.all([
      ruleFormRef.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),

      ruleFormRef4.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
    ]);
    if (!flag) {
      for (const key in ruleForm) {
        if (objError[key]) {
          ruleFormRef.value.scrollToField(key);
          ruleFormRef4.value.scrollToField(key);
          break;
        }
      }
      return;
    }
  }

  if (ruleForm.alarmSettingType === '3') {
    if (!ruleFormRef.value || !ruleFormRef5.value || !ruleFormRef3.value)
      return;
    let flag = true;
    let objError = {} as any;
    await Promise.all([
      ruleFormRef.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
      ruleFormRef5.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
      ruleFormRef3.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
    ]);
    if (!flag) {
      for (const key in ruleForm) {
        if (objError[key]) {
          ruleFormRef.value.scrollToField(key);
          ruleFormRef5.value.scrollToField(key);
          ruleFormRef3.value.scrollToField(key);
          break;
        }
      }
      return;
    }
  }

  if (ruleForm.alarmSettingType === '4') {
    if (!ruleFormRef.value || !ruleFormRef6.value || !ruleFormRef7.value)
      return;
    let flag = true;
    let objError = {} as any;
    await Promise.all([
      ruleFormRef.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
      ruleFormRef6.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
      ruleFormRef7.value.validate((e: any, fields: any) => {
        flag = flag ? e : false;
        if (!e) {
          objError = { ...objError, ...fields };
        }
      }),
    ]);
    if (!flag) {
      for (const key in ruleForm) {
        if (objError[key]) {
          ruleFormRef.value.scrollToField(key);
          ruleFormRef6.value.scrollToField(key);
          ruleFormRef7.value.scrollToField(key);
          break;
        }
      }
      return;
    }
  }

  if (ruleForm.alarmSettingType === '1') {
    const alarmTermsArr = [
      ...ruleForm.dataForm,
      ...ruleForm.carDataForm,
      ...ruleForm.failDataForm,
    ];
    optionsS.alarmTermsArr = alarmTermsArr.filter((item: any) => {
      if (item.threshold === '') {
        delete item.threshold;
        delete item.itemRules;
      }
      return item;
    });
  }

  if (ruleForm.alarmSettingType === '2') {
    const alarmTermsArr = [...ruleForm.accountDataForm];
    optionsS.alarmTermsArr = alarmTermsArr.filter((item: any) => {
      if (item.threshold === '') {
        delete item.threshold;
        delete item.itemRules;
      }
      return item;
    });
  }

  if (ruleForm.alarmSettingType === '3') {
    const alarmTermsArr = [...ruleForm.abnormalForm, ...ruleForm.failDataForm];

    optionsS.alarmTermsArr = alarmTermsArr.filter((item: any) => {
      if (item.threshold === '') {
        delete item.threshold;
        delete item.itemRules;
        item.isNotify = false;
        item.isAlarm = false;
      }
      return item;
    });
  }

  if (ruleForm.alarmSettingType === '4') {
    const alarmTermsArr = [
      ...ruleForm.plieDataForm,
      ...ruleForm.plieFaultDataForm,
    ];
    optionsS.alarmTermsArr = alarmTermsArr.filter((item: any) => {
      if (item.threshold === '') {
        delete item.threshold;
        delete item.itemRules;
      }
      return item;
    });
  }

  // 用户，后面不传值则把分割出来的""去除
  if (ruleForm.notifyNumbers) {
    const index = ruleForm.notifyNumbers.split(',').length;
    if (!ruleForm.notifyNumbers.split(',')[index - 1]) {
      ruleForm.notifyNumbers = ruleForm.notifyNumbers.split(',', index - 1);
    } else {
      ruleForm.notifyNumbers = ruleForm.notifyNumbers.split(',');
    }
  } else {
    ruleForm.notifyNumbers = [];
  }

  const form: any = {
    id,
    alarmSettingName: ruleForm.alarmSettingName,
    alarmSettingType: ruleForm.alarmSettingType,
    relateIds: ruleForm.relateIds,
    notifyNumbers: ruleForm.notifyNumbers,
    alarmTerms: optionsS.alarmTermsArr,
  };
  console.log('optionsS.alarmTermsArr====', optionsS.alarmTermsArr);

  console.log('我来啦~form====', form);

  if (id) {
    updateAlarmSetting(form).then((res: any) => {
      emit('clear-cache');
      back();
    });
  } else {
    saveAlarmSetting(form).then((res: any) => {
      emit('clear-cache');
      back();
    });
  }
};
</script>
<style lang="scss" scoped>
.tips-info {
  background: #c6e2ff;
  padding: 10px;
  margin-top: 10px;

  .info-item {
    font-size: 14px;
    line-height: 28px;
    margin: 0 20px;
  }
}

.message-card {
  border: 1px solid var(--el-color-primary);
  padding: 14px 0;
  margin: 16px;

  .message-title {
    padding-left: 12px;
    font-size: 12px;
    color: var(--el-color-primary);
    font-weight: 500;
  }

  .message-textarea {
    width: 95% !important;
  }

  .card-edit {
    position: absolute;
    right: 5%;
    top: 0;
    margin-right: 10px;
    font-size: 12px;
    cursor: pointer;
  }

  .card-save {
    display: flex;
    position: absolute;
    right: 5%;
    top: 0;
    margin-right: 10px;
    cursor: pointer;
  }
}

.flex-form {
  display: flex;
}
</style>